<template>
  <view id="myCode" class="page">
    <view v-if="codeList.length > 0" style="margin-top: 12px">
      <view class="item" v-for="(item, index) in codeList" :key="index">
        <view class="title">
          <view class="code">挪车码 {{ item.code }}</view>
          <image
            class="img"
            src="../../../image/edit.png"
            @tap="onClickEdit(item)"
          />
        </view>
        <view class="content">
          <view>
            <view class="title-plate">车牌号</view>
            <view class="font">{{ item.plateNo }}</view>
          </view>
          <view>
            <view class="title-plate"> 手机号</view>
            <view class="font">{{ item.mobile }}</view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import Taro from "@tarojs/taro";
import "./myCode.less";
import { setGlobalData, getGlobalData } from "../../../globalData.js";
import { request } from "../../../util/axios.js";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
const API_CarCode = process.env.IOParking.API_CARCODE;
const apiBaseUrl = process.env.IOParking.API_HOST; //请求头部
export default {
  name: "myCode",
  data() {
    return {
      codeList: [],
    };
  },
  //mixins:[authorize],
  components: {},
  mounted() {},

  onLoad: function (options) {
    if (!getGlobalData("openId")) {
      this.wx_login();
    } else {
      this.getCodeList();
    }
  },
  created() {},
  methods: {
    wx_login() {
      api
        .getUserInfo({
          scope: "all",
        })
        .then((resp) => {
          console.log("微信authCode is：", JSON.stringify(resp));
          if (resp.success) {
            if (resp.data == "mock") {
              this.getCodeList();
              return;
            }
            this.getInfos(resp.data);
          } else {
            console.log("err is ", resp.message, "err code is ", resp.code);
          }
        });
    },
    getInfos(authCode) {
      Taro.request({
        url: apiBaseUrl + "/weixin/souchegetuser?authCode=" + authCode,
        data: {},
        header: {
          "content-type": "application/json", // 默认值
        },
        method: "GET",
        success: (res) => {
          console.log("获取用户信息：", res.data);
          if (res.data.isSuccess) {
            var userOpenId = res.data.data.openId;
            setGlobalData("openId", userOpenId);
            this.getCodeList();
          }
        },
      });
    },
    //回到主页
    goHome() {
      api.openPage("home");
    },
    //获取挪车码
    getCodeList() {
      Taro.showLoading({
        title: "加载中",
        mask: true,
      });
      let url =
        API_CarCode + "carmovingcode/alllist?id=" + getGlobalData("openId");
      Taro.request({
        url: url,
        data: {},
        header: {
          "content-type": "application/json", // 默认值
        },
        method: "GET",
        success: (res) => {
          Taro.hideLoading();
          if (res.data.isSuccess) {
            this.codeList = res.data.data;
          }
        },
      });
    },
    onClickEdit(item) {
      console.log("选择条目", JSON.stringify(item));
      Taro.redirectTo({
        url: `../editCode/editCode?no=${encodeURIComponent(item.no)}&plateNo=${
          item.plateNo
        }&mobile=${item.mobile}`,
      });
    },
  },
};
</script>

